
/*request和response的Holder
* 文件名：HttpReqRespHolder.java
* 版权：Copyright by www.ysepay.com
* 修改人：Cindy
* 修改时间：2016年4月6日
* 修改内容：
*/

package com.yspay.sample.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.NamedThreadLocal;

public class HttpReqRespHolder {
	public static final ThreadLocal<HttpServletRequest> httpRequest = new NamedThreadLocal<HttpServletRequest>(
			"http.request.holder");

	public static final ThreadLocal<HttpServletResponse> httpResponse = new NamedThreadLocal<HttpServletResponse>(
			"http.response.holder");

	/**
	 * Reset the HttpContext for the current thread.
	 */
	public static void resetHttpContext() {
		httpRequest.remove();
		httpResponse.remove();
	}

	/**
	 * Associate the given HttpServletRequest&HttpServletResponse with the
	 * current thread
	 */
	public static void setHttpContext(HttpServletRequest httpReq,
			HttpServletResponse httpRes) {
		if (httpReq == null || httpRes == null) {
			resetHttpContext();
		} else {
			httpRequest.set(httpReq);
			httpResponse.set(httpRes);
		}
	}

	public static HttpServletRequest getHttpServletRequest() {
		return httpRequest.get();
	}

	public static HttpServletResponse getHttpServletResponse() {
		return httpResponse.get();
	}
}
